#delimit;
set more off;

capture clear;

capture log close;

set mem 200m;

set logtype text;

log using /hmt/hpcstorage1/hpc/sscc/work/projects/ccm/CCM/CCM-RAND_for_posting_on_Dataverse/log_files/clean_data_log.log, replace;


/*********************************************************************************************************************
*
* This do file cleans the raw data on accidental deaths variables, tort reform variables, and control variables,
* generating new variables that are used in the regressions for the JSL and accidental deaths paper.    
* The dataset produced by this do file has all of the variables that are used to run the regressions
* in the paper.  
* 
* Program by Dan Carvell, written between Summer 2007 and Spring 2010.  
*
*********************************************************************************************************************/

 
 

use "/hmt/hpcstorage1/hpc/sscc/work/projects/ccm/CCM/CCM-RAND_for_posting_on_Dataverse/data/raw_state_year_data/Raw_Data_Deaths.dta", clear;






/* CREATING NEW VARIABLES FOR THE REGRESSIONS  */

/* First, create statefip indicators and state specific time trends for the regressions */


/* state-specific time trends */

capture drop sy*;


gen sy1=    (statefip==1)*(year-1981);
gen sy2=	(statefip==2)*(year-1981);	   
gen sy4=	(statefip==4)*(year-1981);	   
gen sy5=	(statefip==5)*(year-1981);	   
gen sy6=	(statefip==6)*(year-1981);	   
gen sy8=	(statefip==8)*(year-1981);	   
gen sy9=	(statefip==9)*(year-1981);	   
gen sy10=	(statefip==10)*(year-1981);	   
gen sy11=	(statefip==11)*(year-1981);	   
gen sy12=	(statefip==12)*(year-1981);	   
gen sy13=	(statefip==13)*(year-1981);	   
gen sy15=	(statefip==15)*(year-1981);	   
gen sy16=	(statefip==16)*(year-1981);	   
gen sy17=	(statefip==17)*(year-1981);	   
gen sy18=	(statefip==18)*(year-1981);	   
gen sy19=	(statefip==19)*(year-1981);	   
gen sy20=	(statefip==20)*(year-1981);	   
gen sy21=	(statefip==21)*(year-1981);	   
gen sy22=	(statefip==22)*(year-1981);	   
gen sy23=	(statefip==23)*(year-1981);	   
gen sy24=	(statefip==24)*(year-1981);	   
gen sy25=	(statefip==25)*(year-1981);	   
gen sy26=	(statefip==26)*(year-1981);	   
gen sy27=	(statefip==27)*(year-1981);	   
gen sy28=	(statefip==28)*(year-1981);	   
gen sy29=	(statefip==29)*(year-1981);	   
gen sy30=	(statefip==30)*(year-1981);	   
gen sy31=	(statefip==31)*(year-1981);	   
gen sy32=	(statefip==32)*(year-1981);	   
gen sy33=	(statefip==33)*(year-1981);	   
gen sy34=	(statefip==34)*(year-1981);	   
gen sy35=	(statefip==35)*(year-1981);	   
gen sy36=	(statefip==36)*(year-1981);	   
gen sy37=	(statefip==37)*(year-1981);	   
gen sy38=	(statefip==38)*(year-1981);	   
gen sy39=	(statefip==39)*(year-1981);	   
gen sy40=	(statefip==40)*(year-1981);	   
gen sy41=	(statefip==41)*(year-1981);	   
gen sy42=	(statefip==42)*(year-1981);	   
gen sy44=	(statefip==44)*(year-1981);	   
gen sy45=	(statefip==45)*(year-1981);	   
gen sy46=	(statefip==46)*(year-1981);	   
gen sy47=	(statefip==47)*(year-1981);	   
gen sy48=	(statefip==48)*(year-1981);	   
gen sy49=	(statefip==49)*(year-1981);	   
gen sy50=	(statefip==50)*(year-1981);	   
gen sy51=	(statefip==51)*(year-1981);	   
gen sy53=	(statefip==53)*(year-1981);	   
gen sy54=	(statefip==54)*(year-1981);	   
gen sy55=	(statefip==55)*(year-1981);	   
gen sy56=	(statefip==56)*(year-1981);


/* state-specific time trend macro */
global sy "sy2 sy4-sy6 sy8-sy13 sy15-sy42 sy44-sy51 sy53-sy56";







/* Generate state Fixed Effects variables */

capture drop st1 st2 st4 st5 st6 st8 st9 st10 st11 st12 st13 st15 st16 st17 st18 st19 st20 st21 st22 st23 st24 st25 st26 st27 st28 st29 st30 st31 st32 st33 st34 st35 st36 st37 st38 st39 st40 st41 st42 st44 st45 st46 st47 st48 st49 st50 st51 st53 st54 st55 st56;

/* I couldn't have written capture drop st* there, as that would drop variables like statefip and so forth */  

gen st1=    (statefip==1);
gen st2=	(statefip==2);	   
gen st4=	(statefip==4);	   
gen st5=	(statefip==5);	   
gen st6=	(statefip==6);	   
gen st8=	(statefip==8);	   
gen st9=	(statefip==9);	   
gen st10=	(statefip==10);	   
gen st11=	(statefip==11);	   
gen st12=	(statefip==12);	   
gen st13=	(statefip==13);	   
gen st15=	(statefip==15);	   
gen st16=	(statefip==16);	   
gen st17=	(statefip==17);	   
gen st18=	(statefip==18);	   
gen st19=	(statefip==19);	   
gen st20=	(statefip==20);	   
gen st21=	(statefip==21);	   
gen st22=	(statefip==22);	   
gen st23=	(statefip==23);	   
gen st24=	(statefip==24);	   
gen st25=	(statefip==25);	   
gen st26=	(statefip==26);	   
gen st27=	(statefip==27);	   
gen st28=	(statefip==28);	   
gen st29=	(statefip==29);	   
gen st30=	(statefip==30);	   
gen st31=	(statefip==31);	   
gen st32=	(statefip==32);	   
gen st33=	(statefip==33);	   
gen st34=	(statefip==34);	   
gen st35=	(statefip==35);	   
gen st36=	(statefip==36);	   
gen st37=	(statefip==37);	   
gen st38=	(statefip==38);	   
gen st39=	(statefip==39);	   
gen st40=	(statefip==40);	   
gen st41=	(statefip==41);	   
gen st42=	(statefip==42);	   
gen st44=	(statefip==44);	   
gen st45=	(statefip==45);	   
gen st46=	(statefip==46);	   
gen st47=	(statefip==47);	   
gen st48=	(statefip==48);	   
gen st49=	(statefip==49);	   
gen st50=	(statefip==50);	   
gen st51=	(statefip==51);	   
gen st53=	(statefip==53);	   
gen st54=	(statefip==54);	   
gen st55=	(statefip==55);	   
gen st56=	(statefip==56);

global st "st2 st4-st6 st8-st13 st15-st42 st44-st51 st53-st56";












/* Generate new variables for the presence of tort reforms in each state, variables that will be used in the regressions  */


/* The prefix o_ in front of a tort reform variable means that the tort reform applies in tort suits in general - in tort   */
/* suits other than medical malpractice cases, as well as in medical malpractice cases                                      */
/* The prefix m_ in front of a tort reform variable means that the tort reform applies in medical malpractice cases -       */
/* it may or may not be specific to medical malpractice, though.  If the o_ tort reform variable is equal to 0 in a state   */
/* in a year, but the m_ tort reform variable is not, then a tort reform is in place that only applies in medical           */
/* malpractice cases                                                                                                        */






/* Variables for caps on Noneconomic Damages, ie NED caps - in the raw data this is coded as 0 if there is no cap,          */
/* and as the nominal dollar value of the cap if there is a cap in place - turn these variables into indicator variables    */


gen o_nedcap_raw=o_nedcap;
drop o_nedcap;

gen m_nedcap_raw=m_nedcap;
drop m_nedcap;

gen o_nedcap=0;
replace o_nedcap=1 if o_nedcap_raw!=0;

label variable o_nedcap "=1 if cap on NED applying in all or most types of tort suits in general is in place";

gen m_nedcap=0;
replace m_nedcap=1 if m_nedcap_raw!=0;


/* The m_dcap variable is for the presence of a cap on total damages, both economic and noneconomic, that applies       */
/* in medical malpractice suits.  This also must be turned into an indicator variable, and later in this do file        */
/* when variables for the presence of caps on NED that are specific to medical malpractice are generated, these caps    */
/* on total damages will counted as caps on NED for the purpose of generating the variable for caps on NED that are     */
/* specific to med mal                                                                                                  */

gen m_dcap_raw=m_dcap;
drop m_dcap;

gen m_dcap=0;
replace m_dcap=1 if m_dcap_raw!=0;










/* Create variables for the presence of a JSL reform                                                                                                */
/* The variable equals 1 if a JSL reform of any type is in place - that is because in our theoretical model, a solvent tortfeasor has an incentive  */
/* to bring a judgment proof co-tortfeasor into the case regardless of what the cutoff in the particular JSL reform is, the cutoff being the        */
/* percentage threshold below which a tortfeasor is severally liable, and above which the tortfeasor is liable for the entirety of the damages      */


gen o_jsl_0=0; 
replace o_jsl_0=1 if [o_jsled>0 | o_jslned>0];

label variable o_jsl_0 " =1 iff [o_jsled>0 | o_jslned>0], ie a JSL reform is in place";

gen m_jsl_0=0; 
replace m_jsl_0=1 if [m_jsled>0 | m_jslned>0];




/* Create variable for the presence of a JSL reform that applies in all tort suits over auto accidents -               */
/* Hawaii's does not apply to most tortfeasors in auto accidents, with the one exception being that it applies         */
/* to a government agency that maintains the road if the agency did not have reasonable prior notice that a feature    */
/* of the road had led to a prior auto accident under similar circumstances.                                           */
/* Michigan's JSL reform does not apply to drunk driver tortfeasors                                                    */
/* Pennsylvania's JSL reform does not apply to businesses that sell or give alcohol to visibly intoxicated customers   */
/* who go on to injure third parties when they are intoxicated.                                                        */



generate o_jsl_0_auto=o_jsl_0;

** Hawaii, statefip 15;
replace o_jsl_0_auto=0 if statefip==15;

** Michigan, statefip 26;
replace o_jsl_0_auto=0 if statefip==26;

** Pennsylvania, statefip 42;
replace o_jsl_0_auto=0 if statefip==42;














/* CSR Reform Variables */

/* Variable definitions for the CSR variables in the raw data - */

/* o_csr and m_csr indicate the presence of CSR reforms that allow tort awards to be offset by payments from private collateral sources,  */
/* such as private health insurance.  o_csrp and m_csrp indicate the presence of CSR reforms that allow tort awards to be offset by       */
/* payments from public collateral sources, such as publicly provided health insurance.  Although both of these types of variables are    */
/* in the raw data, in this paper, we only study the effects of CSR reforms that allow awards to be offset by payments from private       */
/* collateral sources, since private insurance is so common, and so that these reforms should have larger effects.  However, during the   */
/* time period we study, only one state (Colorado) enacts a CSR reform that applies in tort suits in general, not just medical            */
/* malpractice cases, and that only allows offsets of payments from public collateral sources, and not private ones.  None of the states  */
/* strike down or repeal such reforms                                                                                                     */

/* The suffix _off indicates that the CSR reform requires tort awards to be offset by collateral source payments.  The suffix _ev         */
/* indicates that a CSR reform is in place that allows tort awards to be offset by collateral source payments at the discretion of the    */
/* trier of fact in the case.  Since the latter type of CSR reform is uncommon, however, I group these two types of reforms together in   */
/* the CSR reform variable used in the regressions                                                                                        */



label variable o_csr_off "=1 iff mandatory offset CSR reform";
label variable o_csr_ev "=1 iff discretionary offset CSR reform";
label variable m_csr_off "=1 iff mandatory offset CSR reform";
label variable m_csr_ev "=1 iff discretionary offset CSR reform";


gen o_csr=0;
replace o_csr=1 if (o_csr_off==1 | o_csr_ev==1);
gen m_csr=0;
replace m_csr=1 if (m_csr_off==1 | m_csr_ev==1);

label variable o_csr "any generally applicable CSR reform, mandatory or discretionary";
label variable m_csr "any CSR reform that applies in med mal cases, mandatory or discretionary";










/* Generate variables for the presense of tort reforms that are specific to medical malpractice - i.e. reforms that only apply in med mal suits  */



/* In the paper, we only ended up studying the effects of NED caps that are specific to med mal, as it's rare for other types of tort reforms    */
/* besides NED caps to be specific to med mal.  A few states have caps on total damages, both noneconomic and economic, that apply only in med   */
/* mal suits.  We consider these caps on total damages to be equivalent to NED caps, as both types of caps are strong caps that serve the same   */
/* purpose.  So in order to generate the variable for caps on NED that are specific to med mal, I need to replace the variable m_nedcap with 1   */
/* when a cap on total damages applying in med mal claims is in place in a state                                                                 */


replace m_nedcap=1 if (m_dcap==1 | m_nedcap==1);


gen m_nedcap_2=m_nedcap;
gen o_nedcap_2=o_nedcap;


gen mms_nedcap= m_nedcap - o_nedcap;
replace mms_nedcap=0 if (m_nedcap==0 & o_nedcap==1);

label variable mms_nedcap "=1 iff there is a cap on NED that is specific to med mal";
















/* We also have variables for no-fault auto insurance to use as controls in the regressions with auto accidents as the dependent variable     */
/* There are two basic types of no-fault auto insurance laws - regular no-fault auto insurance, in which tort liability is replaced with      */
/* no-fault first party compensation for auto accidents in which damages fall below a certain threshold, and add-on auto insurance, in which  */
/* accident victims receive compensation for their own harms from their own insurer, but face no restrictions on their right and ability to   */
/* sue other drivers to recover even more compensation.  The coding of the two variables for no-fault auto in the raw data reflects these     */
/* distinctions.  However, I also need to generate a variable indicating the presence of any kind of no-fault auto insurance law, either      */
/* regular or add-on                                                                                                                          */




generate no_fault_auto_any=0;
replace no_fault_auto_any=1 if (no_fault_auto_add_on==1 | no_fault_auto_regular==1);
























/* Now, I need to make variables for the presence of "New" and "Old" Tort reforms - variables for new tort reforms that were enacted    */
/* within the last 2, 3, or 4 years, and variables for old tort reforms that have been in place for more than 2, 3, or 4 years          */
/* Making all of these variables takes many lines of commands, and is a large component of this do file                                 */




/* First, I am making the NED cap variables.  Recall that the variable o_nedcap_enact gives the year in which the NED cap was enacted,  */
/* for state-year observations with an NED cap in place.  The difference between the current year and o_nedcap_enact gives the number   */
/* of years that the NED cap has been in place for                                                                                      */


gen o_ned_year= year - o_nedcap_enact;

/* The suffix _yr2 on these variables means the reform has been in place for more than 2 years    */

gen o_nedcap_yr2=0;
replace o_nedcap_yr2=1 if (o_nedcap==1 & o_ned_year>1 & o_ned_year!=.);

gen m_ned_year= year - m_nedcap_enact;
gen m_nedcap_yr2=0;
replace m_nedcap_yr2=1 if (m_nedcap==1 & m_ned_year>1 & m_ned_year!=.);

** Make mms_nedcap_yr2 from these two new variables;
gen mms_nedcap_yr2= m_nedcap_yr2 - o_nedcap_yr2;
replace mms_nedcap_yr2=0 if (m_nedcap_yr2==0 & o_nedcap_yr2==1);


** Now the indicators for if there is a reform that has been in place for 0 or 1 years;
gen o_nedcap_new=0;
replace o_nedcap_new=1 if (o_nedcap==1 & (o_ned_year==1 | o_ned_year==0));
gen m_nedcap_new=0;
replace m_nedcap_new=1 if (m_nedcap==1 & (m_ned_year==1 | m_ned_year==0));
gen mms_nedcap_new= m_nedcap_new - o_nedcap_new;
replace mms_nedcap_new=0 if (m_nedcap_new==0 & o_nedcap_new==1);




/* Create other variables for new and old reforms, looking at the effects of the law in the first 3 or 4 years vs. after year 3 or 4  */


gen o_nedcap_yr3=0;
replace o_nedcap_yr3=1 if (o_nedcap==1 & o_ned_year>2 & o_ned_year!=.);
gen m_nedcap_yr3=0;
replace m_nedcap_yr3=1 if (m_nedcap==1 & m_ned_year>2 & m_ned_year!=.);

gen o_nedcap_yr4=0;
replace o_nedcap_yr4=1 if (o_nedcap==1 & o_ned_year>3 & o_ned_year!=.);
gen m_nedcap_yr4=0;
replace m_nedcap_yr4=1 if (m_nedcap==1 & m_ned_year>3 & m_ned_year!=.);





gen mms_nedcap_yr3= m_nedcap_yr3 - o_nedcap_yr3;
replace mms_nedcap_yr3=0 if (m_nedcap_yr3==0 & o_nedcap_yr3==1);

gen mms_nedcap_yr4= m_nedcap_yr4 - o_nedcap_yr4;
replace mms_nedcap_yr4=0 if (m_nedcap_yr4==0 & o_nedcap_yr4==1);



** Now the indicators for if the reform has been in place for only 2 or 3 years, i.e. enacted within the last 3 or 4 years;


gen o_nedcap_new3=0;
replace o_nedcap_new3=1 if (o_nedcap==1 & (o_ned_year>=0 &  o_ned_year<=2));
gen m_nedcap_new3=0;
replace m_nedcap_new3=1 if (m_nedcap==1 & (m_ned_year>=0 & m_ned_year<=2));
gen mms_nedcap_new3= m_nedcap_new3 - o_nedcap_new3;
replace mms_nedcap_new3=0 if (m_nedcap_new3==0 & o_nedcap_new3==1);

gen o_nedcap_new4=0;
replace o_nedcap_new4=1 if (o_nedcap==1 & (o_ned_year>=0 & o_ned_year<=3));
gen m_nedcap_new4=0;
replace m_nedcap_new4=1 if (m_nedcap==1 & (m_ned_year>=0 & m_ned_year<=3));
gen mms_nedcap_new4= m_nedcap_new4 - o_nedcap_new4;
replace mms_nedcap_new4=0 if (m_nedcap_new4==0 & o_nedcap_new4==1);





/* Generate two-year leads of the NED cap variables - variables equal 1 in the two years before the reform gets enacted, and are 0 otherwise   */
/* The variable o_nedcap_enact_lead states the year in which a reform that will be enacted in the future is enacted                            */


gen o_nedcap_year_lead=o_nedcap_enact_lead - year;

gen o_nedcap_lead_2=0;
replace o_nedcap_lead_2=1 if (o_nedcap_year_lead==1 | o_nedcap_year_lead==2);


gen m_nedcap_year_lead=m_nedcap_enact_lead - year;

gen m_nedcap_lead_2=0;
replace m_nedcap_lead_2=1 if (m_nedcap_year_lead==1 | m_nedcap_year_lead==2);


gen mms_nedcap_lead_2=0;
replace mms_nedcap_lead_2=1 if (o_nedcap_lead_2==0 & m_nedcap_lead_2==1);






/* Variables for new and old Punitive Damages Reforms - in the paper we use a composite Punitive Damages Reform variable that equals 1 if either  */
/* a Punitive Damages Cap or a Punitive Damages Evidence reform is in place - but I've left the commands to create variables for new and old      */
/* PD caps and PD reforms in this program, in case they are of any use in the future, and as they've already been written                         */



gen o_pd_year= year - o_pdcap_enact;

gen o_pdcap_yr2=0;
replace o_pdcap_yr2=1 if (o_pdcap==1 & o_pd_year>1 & o_pd_year!=.);


gen o_pdcap_new=0;
replace o_pdcap_new=1 if (o_pdcap==1 & (o_pd_year==1 | o_pd_year==0));






/* Now 3 and 4 years rather than 2 years as the cutoff between new and old reforms  */

gen o_pdcap_yr3=0;
replace o_pdcap_yr3=1 if (o_pdcap==1 & o_pd_year>2 & o_pd_year!=.);


gen o_pdcap_new3=0;
replace o_pdcap_new3=1 if (o_pdcap==1 & (o_pd_year>=0 & o_pd_year<=2));



gen o_pdcap_yr4=0;
replace o_pdcap_yr4=1 if (o_pdcap==1 & o_pd_year>3 & o_pd_year!=.);


gen o_pdcap_new4=0;
replace o_pdcap_new4=1 if (o_pdcap==1 & (o_pd_year>=0 & o_pd_year<=3));





/* Generate 2 - year leads of the PD Cap variables    */


gen o_pdcap_year_lead=o_pdcap_enact_lead - year;


gen o_pdcap_lead_2=0;
replace o_pdcap_lead_2=1 if (o_pdcap_year_lead==1 | o_pdcap_year_lead==2);









/*  Now, create variables for new and old PD Evidence reforms, and the leads of the PD Evidence Reform variables   */


gen o_pdev_year= year - o_pd_ev_enact;

gen o_pdev_yr2=0;
replace o_pdev_yr2=1 if (o_pdev==1 & o_pdev_year>1 & o_pdev_year!=.);


gen o_pdev_new=0;
replace o_pdev_new=1 if (o_pdev==1 & (o_pdev_year==1 | o_pdev_year==0));




/* Now 3 and 4 years rather than 2 years as the cutoff between new and old reforms  */




gen o_pdev_yr3=0;
replace o_pdev_yr3=1 if (o_pdev==1 & o_pdev_year>2 & o_pdev_year!=.);


gen o_pdev_new3=0;
replace o_pdev_new3=1 if (o_pdev==1 & (o_pdev_year>=0 & o_pdev_year<=2));



gen o_pdev_yr4=0;
replace o_pdev_yr4=1 if (o_pdev==1 & o_pdev_year>3 & o_pdev_year!=.);


gen o_pdev_new4=0;
replace o_pdev_new4=1 if (o_pdev==1 & (o_pdev_year>=0 & o_pdev_year<=3));




/* Two-year lead of the PD - evidence variable     */

gen o_pdev_year_lead=o_pd_ev_enact_lead - year;

gen o_pdev_lead_2=0;
replace o_pdev_lead_2=1 if (o_pdev_year_lead==1 | o_pdev_year_lead==2);








/* Variables for new and old PD Reforms - either a PD Cap or a PD Evidence Reform is in place - and the 2-Year Lead of the PD Reform Variable   */


gen o_pdr_year= year - o_pd_reform_enact;

gen o_pd_reform_yr2=0;
replace o_pd_reform_yr2=1 if (o_pd_reform==1 & o_pdr_year>1 & o_pdr_year!=.);

gen o_pd_reform_new=0;
replace o_pd_reform_new=1 if (o_pd_reform==1 & (o_pdr_year==1 | o_pdr_year==0));



/* Now 3 and 4 years rather than 2 years as the cutoff between new and old reforms  */



gen o_pd_reform_yr3=0;
replace o_pd_reform_yr3=1 if (o_pd_reform==1 & o_pdr_year>2 & o_pdr_year!=.);

gen o_pd_reform_new3=0;
replace o_pd_reform_new3=1 if (o_pd_reform==1 & (o_pdr_year>=0 & o_pdr_year<=2));


gen o_pd_reform_yr4=0;
replace o_pd_reform_yr4=1 if (o_pd_reform==1 & o_pdr_year>3 & o_pdr_year!=.);

gen o_pd_reform_new4=0;
replace o_pd_reform_new4=1 if (o_pd_reform==1 & (o_pdr_year>=0 & o_pdr_year<=3));



/* Lead of the PD-Reform Variable   */ 

gen o_pdr_year_lead=o_pd_reform_enact_lead - year;

gen o_pd_reform_lead_2=0;
replace o_pd_reform_lead_2=1 if (o_pdr_year_lead==1 | o_pdr_year_lead==2);










/* Now, create variables for new and old CSR reforms, and the 2-year leads of the CSR reform variable        */


gen o_csr_year= year - o_csr_enact;
gen o_csr_yr2=0;
replace o_csr_yr2=1 if (o_csr==1 & o_csr_year>1 & o_csr_year!=.);


gen o_csr_new=0;
replace o_csr_new=1 if (o_csr==1 & (o_csr_year==1 | o_csr_year==0));


/* Now 3 and 4 years rather than 2 years as the cutoff between new and old reforms  */



gen o_csr_yr3=0;
replace o_csr_yr3=1 if (o_csr==1 & o_csr_year>2 & o_csr_year!=.);


gen o_csr_new3=0;
replace o_csr_new3=1 if (o_csr==1 & (o_csr_year>=0 & o_csr_year<=2));


gen o_csr_yr4=0;
replace o_csr_yr4=1 if (o_csr==1 & o_csr_year>3 & o_csr_year!=.);


gen o_csr_new4=0;
replace o_csr_new4=1 if (o_csr==1 & (o_csr_year>=0 & o_csr_year<=3));




/* Two-year lead of the variable        */

gen o_csr_year_lead=o_csr_enact_lead - year;


gen o_csr_lead_2=0;
replace o_csr_lead_2=1 if (o_csr_year_lead==1 | o_csr_year_lead==2);











/* Now, create variables for new and old JSL reforms, and the 2 year lead of the JSL reform variable as well      */



gen o_jsl_0_year= year - o_jsl_0_enact;


/* First, a two-year cutoff between new and old JSL reforms         */
gen o_jsl_0_new=0;
replace o_jsl_0_new=1 if (o_jsl_0==1 & (o_jsl_0_year==1 | o_jsl_0_year==0));
gen o_jsl_0_yr2=0;
replace o_jsl_0_yr2=1 if (o_jsl_0==1 & o_jsl_0_year>1 & o_jsl_0_year!=.);

/* Now, a three-year cutoff      */

gen o_jsl_0_yr3=0;
replace o_jsl_0_yr3=1 if (o_jsl_0==1 & o_jsl_0_year>2 & o_jsl_0_year!=.);
gen o_jsl_0_new3=0;
replace o_jsl_0_new3=1 if (o_jsl_0==1 & (o_jsl_0_year>=0 & o_jsl_0_year<=2));

/* Now, a four-year cutoff       */

gen o_jsl_0_yr4=0;
replace o_jsl_0_yr4=1 if (o_jsl_0==1 & o_jsl_0_year>3 & o_jsl_0_year!=.);
gen o_jsl_0_new4=0;
replace o_jsl_0_new4=1 if (o_jsl_0==1 & (o_jsl_0_year>=0 & o_jsl_0_year<=3));





/* Now, make the two-year lead of the JSL reform variable    */


gen o_jsl_0_year_lead=o_jsl_0_enact_lead - year;

gen o_jsl_0_lead_2=0;
replace o_jsl_0_lead_2=1 if (o_jsl_0_year_lead==1 | o_jsl_0_year_lead==2);
































/* Now, generate the dependent variables - accidental death rates                       */
/* Prior drafts of this paper used dependent variables that were logs of accidental     */
/* death rates - I have kept the commands to make the logs of the dependent varaibles   */
/* in this do file, to facilitate easy comparison with the earlier drafts               */



/* generate accidental death rate excluding auto accidents, and the log of this */

gen deathrate= with_auto_raw - just_auto_raw;
gen ln_rate= ln(deathrate);

label variable deathrate "nonautomotive accidental death rate per 100,000 residents";
label variable ln_rate "natural log of deathrate";





/* Generate log of rate of auto accidents only */

gen ln_auto_rate= ln(just_auto_raw);

label variable ln_auto_rate "log of death rate from auto accidents per 100,000 state residents";





/* Generate log of rate of overdoses on illegal drugs and abused pharmaceuticals only,       */
/* as well as the rate of accidents excluding overdoses as well as auto accidents            */


gen ln_OD_rate = ln(OD_rate);

label variable OD_rate "rate of fatal drug overdoses per 100,000 state residents";
label variable ln_OD_rate "log of rate of fatal drug overdoses per 100,000 state residents";


gen deathrate_no_OD= with_auto_raw - just_auto_raw - OD_rate;
gen ln_rate_no_OD= ln(deathrate_no_OD);

label variable deathrate_no_OD "non-auto non-overdose accidental death rate per 100,000 residents";
label variable ln_rate_no_OD "natural log of deathrate_no_OD";



sum OD_rate;
sum deathrate_no_OD;














** Generate even more specific accident rate variables for different categories defined by type of accident;


** First, fall-related deaths;
** This variable is based on a count of falls deaths made with the Vital Stats data;


** Falls - here, VS stands for Vital Statistics;

generate fall_VS_rate= (fall_count_VS/population)*100000;
gen ln_fall_VS=ln(fall_VS_rate);





    
** Drownings;

generate drowning_VS_rate= (drowning_count_VS/population)*100000;
gen ln_drowning_VS=ln(drowning_VS_rate);





    
** Suffocations;

generate suffocate_VS_rate= (suffocate_count_VS/population)*100000;
gen ln_suffocate_VS=ln(suffocate_VS_rate);




    
** Fires and burns;

generate fire_burn_VS_rate= (fire_burn_count_VS/population)*100000;
gen ln_fire_burn_VS=ln(fire_burn_VS_rate);



    
** Poisonings other than drug overdoses;

generate poisonNotOD_VS_rate= (poisonNotOD_count_VS/population)*100000;
gen ln_poisonNotOD_VS=ln(poisonNotOD_VS_rate);




    
** Poisonings by and exposure to other and unspecified solid and liquid substances, which is a subcategory of the poisonings that are not Drug Overdoses;

generate poisonOther_VS_rate= (poisonOther_count_VS/population)*100000;
gen ln_poisonOther_VS=ln(poisonOther_VS_rate);





    
** Poisonings by and exposure to gases and vapors, which is another subcategory of the poisonings that are not Drug Overdoses;

generate poisonGas_VS_rate= (poisonGas_count_VS/population)*100000;
gen ln_poisonGas_VS=ln(poisonGas_VS_rate);





    
** Transportation accidents other than auto accidents;

generate o_trnsprt_VS_rate= (o_trnsprt_count_VS/population)*100000;
gen ln_o_trnsprt_VS=ln(o_trnsprt_VS_rate);




    
** Adverse effects of medical care and pharmaceuticals in therapeutic use;

generate adverseFX_VS_rate= (adverseFX_count_VS/population)*100000;
gen ln_adverseFX_VS=ln(adverseFX_VS_rate);




    
** Cut and pierce injury accidents;

generate cut_prc_VS_rate= (cut_prc_count_VS/population)*100000;
gen ln_cut_prc_VS=ln(cut_prc_VS_rate);




    
** Firearm accidents;

generate firearm_VS_rate= (firearm_count_VS/population)*100000;
gen ln_firearm_VS=ln(firearm_VS_rate);




    
** Machinery accidents;

generate machinery_VS_rate= (machinery_count_VS/population)*100000;
gen ln_machinery_VS=ln(machinery_VS_rate);




    
** Natural disaster and environmental accidents;

generate natural_D_VS_rate= (natural_D_count_VS/population)*100000;
gen ln_natural_D_VS=ln(natural_D_VS_rate);




    
** Overexertion accidents;


generate overexert_VS_rate= (overexert_count_VS/population)*100000;
gen ln_overexert_VS=ln(overexert_VS_rate);




    
** Struck by or against objects accidents;

generate struck_by_VS_rate= (struck_by_count_VS/population)*100000;
gen ln_struck_by_VS=ln(struck_by_VS_rate);








** Janet does not want me to have the logs of 0's be missing values - she wants me to replace the logs of 0's with the logs of 0.001, so we don't throw out observations;



foreach i in ln_fall_VS ln_drowning_VS ln_suffocate_VS ln_fire_burn_VS ln_poisonNotOD_VS ln_poisonOther_VS ln_poisonGas_VS ln_o_trnsprt_VS ln_adverseFX_VS ln_cut_prc_VS ln_firearm_VS ln_machinery_VS ln_natural_D_VS ln_overexert_VS ln_struck_by_VS {;


	replace `i'=ln(0.001) if (`i'==. & year!=2005);
	
	sum `i';


	};




























/* Generate state-year control variables used in the regressions from the raw data on control variables    */




/* First variable to create - hospital beds per capita                                                                                          */
/* Data on hospital beds in the state is not available for every year - so I need to interpolate and extrapolate this data to cover every year  */


sort statefip;
by statefip:  ipolate hospbeds year, gen(hospbeds_intp) epolate;
gen hospbeds_capita = hospbeds_intp/population;

label variable hospbeds_intp "hospital beds in state missing values interpolated";
label variable hospbeds_capita "hospbeds_intp over state population";



/* Data on per capita alcohol consumption is also not available for the year 2005   */

sort statefip;
by statefip:  ipolate per_capita_alcohol year, gen(per_capita_alcohol_intp) epolate;

drop per_capita_alcohol;
rename per_capita_alcohol_intp per_capita_alcohol;

label variable per_capita_alcohol "per capita alcohol consumption";






/* Real per capita income in the state                  */

gen real_income = (nominal_income/CPI_82_84_base)*100;
label variable real_income "real per capital income in the state";




/* Percent of the state population that is African-American     */

gen prct_Afr_Amer = (pop_total_blacks/population)*100;
label variable prct_Afr_Amer "percent of state population that is African-American";



/* Percent of the state population that is Other Minority - which, following the variable definition used by Rubin and Shepherd,     */
/* is Asians and Native Americans, but not Hispanics                                                                                 */ 


gen prct_other_minority = (pop_other_minority/population)*100;
label variable prct_other_minority "percent of state population that is other minority";




/* Percent of the state population that is age 4 or under     */

gen prct_under_four = (pop_age_0_4/population)*100;
label variable prct_under_four "percent of state population that is age 4 or younger";





/* Percent of the state population that is elderly, ie age 65 or above    */


gen prct_elderly = (pop_65_or_over/population)*100;
label variable prct_elderly "percentage of state population age 65 or older";



/* Percent of the state population that are males between the ages of 15 and 24     */



gen prct_young_males= (pop_males_age_15_to_24/population)*100;
label variable prct_young_males "percentage of state population that are males between 15 and 24";















/*  Code to generate dependent variables for the accidental death rates in different age groups                                                       */
/*  Remember that the accidents in these rates are accidents other than auto accidents and drug overdoses                                             */
/*  The raw data on these deaths from the Vital Statistics data is already constructed to exclude deaths from auto accidents and drug overdoses       */


/* Rate for persons age 0 to 5      */

gen rate_ages_0_5= (ages_0_to_5_count/ages_0_to_5)*100000;
gen ln_rate_ages_0_5 = ln(rate_ages_0_5);
replace ln_rate_ages_0_5=0 if rate_ages_0_5==0;

sum rate_ages_0_5;



/* Rate for persons age 6 to 17     */

gen rate_ages_6_17= (ages_6_to_17_count/ages_6_to_17)*100000;
gen ln_rate_ages_6_17 = ln(rate_ages_6_17);
replace ln_rate_ages_6_17=0 if rate_ages_6_17==0;

sum rate_ages_6_17;





/*  Rate for persons age 18 to 64    */

gen rate_ages_18_64= (ages_18_to_64_count/ages_18_to_64)*100000;
gen ln_rate_ages_18_64 = ln(rate_ages_18_64);


sum rate_ages_18_64;





/*  Rate for persons age 65 and above  */

gen rate_ages_65_above= (age_65_or_above_count/age_65_or_above)*100000;
gen ln_rate_ages_65_above = ln(rate_ages_65_above);


sum rate_ages_65_above;












compress;


sort statefip year;






/* This program has now generated all of the variables needed for the regressions - the data set can now be saved under a new name   */  


save "/hmt/hpcstorage1/hpc/sscc/work/projects/ccm/CCM/CCM-RAND_for_posting_on_Dataverse/data/cleaned_data_for_regressions/Clean_Data_Deaths.dta", replace;






















/* Now, generate the Summary Statistics for all of the variables I have                                                       */
/* These commands are to make Summary Statistics for Table 4 in the paper                                                     */

/* I need summary statistics weighted by state-year populations, for the years 1981-2004, the years of data for the paper     */

drop if year>2004;
drop if year<1981;
sum year;


summarize [aweight = population];


describe;







sort o_jsl_0;

** Rate of all types of accidental deaths;
by o_jsl_0: sum with_auto_raw [aweight = population]; 

** Rate of drug overdoses only;
by o_jsl_0: sum OD_rate [aweight = population];

** Rate of auto accidents only;
by o_jsl_0: sum just_auto_raw [aweight = population];

** Rate excluding auto accidents but including drug overdoses;
by o_jsl_0: sum deathrate [aweight = population]; 

** excluding auto accidents as well as drug overdoses;
by o_jsl_0: sum deathrate_no_OD [aweight = population]; 

** rate of accidental deaths excluding auto accidents and overdoses, persons pronounced dead outside of hospitals;
by o_jsl_0: sum at_hosp_rate [aweight = population];

** rate of accidental deaths excluding auto accidents and overdoses, persons pronounced dead at hospitals;
by o_jsl_0: sum not_at_hosp_rate [aweight = population];



** Rates of accidental deaths excluding auto accidents and overdoses, by age group;
by o_jsl_0: sum rate_ages_0_5  [aweight = population]; 
by o_jsl_0: sum rate_ages_6_17 [aweight = population]; 
by o_jsl_0: sum rate_ages_18_64 [aweight = population]; 
by o_jsl_0: sum rate_ages_65_above [aweight = population]; 



** Summary stats for the state-year level control variables;
by o_jsl_0: sum unemp_rate [aweight = population]; 
by o_jsl_0: sum real_income [aweight = population]; 
by o_jsl_0: sum prct_Afr_Amer [aweight = population]; 
by o_jsl_0: sum prct_other_minority [aweight = population]; 
by o_jsl_0: sum prct_under_four [aweight = population]; 
by o_jsl_0: sum prct_elderly [aweight = population]; 
by o_jsl_0: sum prct_young_males [aweight = population]; 
by o_jsl_0: sum per_capita_alcohol [aweight = population]; 
by o_jsl_0: sum hospbeds_capita [aweight = population]; 




    
      











log close;

